package server;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

public class ReceiveThread extends Thread {
	Socket connectionSocket ;
	BufferedReader inFromClient ;	
	DataOutputStream outToClient ;
	
	public ReceiveThread(Socket connectionSocket_var) throws IOException{
		System.out.println("\nnew ReceiveThread for host" + connectionSocket_var.getInetAddress());
		
		connectionSocket = connectionSocket_var;		
		inFromClient = 
			new BufferedReader(new 
					InputStreamReader(connectionSocket.getInputStream()));
		outToClient = 
			new DataOutputStream(connectionSocket.getOutputStream());			
	}
	
	public void run(){
		try{
			while(true){
				String clientMsg = inFromClient.readLine();
				
				if(clientMsg !=null){
					System.out.println("receive: " + clientMsg + " from host " + connectionSocket.getInetAddress());
					if(clientMsg.equals("request_STOP")){
						this.interrupt();
						break;
					}					
				}
			}
		}
		catch (IOException e) { 
			System.out.println("IOException: " + e);			
			this.interrupt();
		} 
	}
}
